package nc.bs.gl.eventlistener.bmcontrol;

import com.yonyou.cloud.utils.StringUtils;
import nc.bs.gl.voucher.VoucherBO;
import nc.vo.gl.pubinterface.VoucherOperateInterfaceVO;
import nc.vo.gl.pubinterface.VoucherSaveInterfaceVO;
import nc.vo.gl.pubvoucher.OperationResultVO;
import nc.vo.gl.pubvoucher.VoucherVO;
import nc.vo.pub.BusinessException;

public class VoucherReBusiCtrl {

    /**
     * 凭证预算控制：单据重算
     * @throws BusinessException
     */
    public void doGlVoucherReBusiCtrl(String[] voucherIds) throws BusinessException{

        VoucherVO voucher = null;

        try {

            if (voucherIds != null) {

                VoucherBO bo = new VoucherBO();
//                VoucherVO[] vouchers = bo.queryByPks(voucherIds);

                //保存动作
                VoucherSaveInterfaceVO saveParam = new VoucherSaveInterfaceVO();
                BmVoucherSave save = new BmVoucherSave();

                //作废动作
                VoucherOperateInterfaceVO abandonParam = new VoucherOperateInterfaceVO();
                BmVoucherAbandon abandon = new BmVoucherAbandon();

                //审批动作
                VoucherOperateInterfaceVO auditParam = new VoucherOperateInterfaceVO();
                BmVoucherAudit audit = new BmVoucherAudit();

                OperationResultVO[] result = null;
                int index = -1;
                for(String pk_voucher : voucherIds){

                    index++;

                    voucher = bo.queryByPk(pk_voucher);

                    /*
                     * 执行保存
                     * 所有凭证执行一遍保存执行预算
                     */
                    saveParam.setVoucher(voucher);
                    result = save.afterSave(saveParam);

                    /*
                     * 执行作废
                     * 针对作废凭证执行一遍作废执行预算
                     */
                    if (voucher.getDiscardflag() != null && voucher.getDiscardflag().booleanValue()) {
                        abandonParam.pk_vouchers = new String[]{voucher.getPk_voucher()};
                        abandonParam.userdata = new Object[]{"VoucherReBusiCtrl",voucher};
                        result =  abandon.afterAbandon(abandonParam);
                    }

                    /*
                     * 执行审批
                     * 针对审批通过的凭证执行一遍审批执行预算
                     */
                    if (StringUtils.isNotEmpty(voucher.getPk_checked())) {
                        auditParam.pk_vouchers = new String[]{voucher.getPk_voucher()};
                        auditParam.userdata = new Object[]{"VoucherReBusiCtrl",voucher};
                        result = audit.afterAudit(auditParam);
                    }

                    if(result != null && result.length > 0){

                        throw new BusinessException(result[0].m_strDescription);

                    }

                }

            }

        }catch(Exception e){

            String mes = "";
            if(voucher != null)
                mes = "凭证日期："+voucher.getPrepareddate()+"、凭证号："+voucher.getNo()+"\n";

            throw new BusinessException(mes+e.getMessage());

        }

    }

}
